<?php

if( !defined("IN_ELCIUS") ) exit;

$recruitment = array(
	array('Class'=>'Priest',		'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Paladin',		'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Rogue',			'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Mage',			'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Shaman',		'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Warrior',		'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Warlock',		'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Hunter',		'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Deathknight',	'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Monk',			'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no'),
	array('Class'=>'Druid',			'Spec1'=>'no','Spec2'=>'no','Spec3'=>'no','Spec4'=>'no')
);

// TODO: add more data? class ID, spec names, etc
// TODO: maybe use a file access manager


// pull recruit status settings from file
if( file_exists( './data/recruiting.txt' ) and is_readable('./data/recruiting.txt') ){
	
	// fetch raw status from cache file
	$cache_raw = @file_get_contents( './data/recruiting.txt' );
	$recruit_status = array();
	$cache_raw = explode( "\n", $cache_raw );
	foreach( $cache_raw as $status ){
		$status = explode( ',', $status );
		$recruit_status[ $status[0] ] = array_slice( $status, 1 );
	}
	unset( $cache_raw, $status );
	
	// update recruitment using the data from the file
	foreach( $recruitment as $i => $status ){
		if( !isset( $recruit_status[ $status['Class'] ] ) ) continue; // current class not found in cache
		// loop through cached specs
		for( $spec = 1; $spec <= count( $recruit_status[ $status['Class'] ] ); $spec++ ){
			if( isset( $status['Spec'.$spec] ) ){ // spec exists
				$recruitment[$i]['Spec'.$spec] = ($recruit_status[ $status['Class'] ][$spec-1]=='yes'?'yes':'no');
			}
		}
	}
	
	// editing
	if( KiwiAuth::IsEditor() === true and is_writable('./data/recruiting.txt') ) {
		$setRecruit = GetRequestVar('SetRecruit');
		if( isset( $setRecruit ) ) {
			list( $class, $spec ) = str_split( $setRecruit, strlen( $setRecruit )-1 );
			
			$temp = '';
			foreach( $recruitment as $i => $status ) {
				if( !empty( $status['Class'] ) ) {
					if( $status['Class'] == $class ){
						$status['Spec'.$spec] = ($status['Spec'.$spec]=='no'?'yes':'no');
						$recruitment[$i] = $status;
					}
					$temp .= "\n".implode( ',', $status );
				}
			}
			file_put_contents('./data/recruiting.txt',substr( $temp, 1 ));
		}
	}
	
}

$PageData['Recruitment'] = $recruitment;

?>